package cn.ante.system.controller;
import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.system.domain.DictionaryType;
import cn.ante.system.query.DictionaryTypeQuery;
import cn.ante.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典类型的api",description="数据字典类型相关的crud功能")
@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
    * @Description: 查询数据字典类型列表
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/15 13:23
    */
    @GetMapping
    public List<DictionaryType> loadAll(){
        return dictionaryTypeService.loadAll();
    }

    @GetMapping("{id}")
    @ApiOperation(value = "通过id查询数据字典类型")
    @PreAuthorize(name ="id查询数据字典类型",sn="dictionaryType:get")
    public DictionaryType loadById(@PathVariable("id") Long id){
        return dictionaryTypeService.loadById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页数据字典类型列表")
    @PreAuthorize(name ="数据字典类型分页展示",sn="dictionaryType:list")
    public PageList<DictionaryType> findAll(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){

        return dictionaryTypeService.findAll(dictionaryTypeQuery);
    }

    @PutMapping
    @ApiOperation(value = "保存/修改数据字典类型")
    @PreAuthorize(name ="保存/修改数据字典类型",sn="dictionaryType:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() == null){
                dictionaryTypeService.add(dictionaryType);
            }else{
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除数据字典类型")
    @PreAuthorize(name ="根据id删除数据字典类型",sn="dictionaryType:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try{
            dictionaryTypeService.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除数据字典类型")
    @PreAuthorize(name ="批量删除数据字典类型",sn="dictionaryType:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try{
            dictionaryTypeService.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
